package com.atguigu.datastream.day03;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * ClassName: Flink_Source_Kafka
 * Package: com.atguigu.day03
 * Description:
 *                     keyBy
 * @Author ChenJun
 * @Create 2023/4/8 16:40
 * @Version 1.0
 */
public class Flink_09_Transformat_keyBy {
    public static void main(String[] args) throws Exception {
        //1.获取Flink 流的运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(4);

        // 2 . 从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //TODO 将读进来的字符串转为Event
        SingleOutputStreamOperator<String> map = streamSource.map(new MapFunction<String, String>() {
            @Override
            public String map(String s) throws Exception {
                return s;
            }
        }).setParallelism(2);

        KeyedStream<String, String> keyedStream = map.keyBy(new KeySelector<String, String>() {
            @Override
            public String getKey(String s) throws Exception {
                return s;
            }
        });

        map.print("原始分区").setParallelism(2);
        keyedStream.print("keyBy分区");


        env.execute();
    }
}
